<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>reduce方法</title>
	</head>
	<body>
		<!-- reduce() 方法对数组中的每个元素按序执行一个由您提供的 reducer 函数，
            每一次运行 reducer 会将先前元素的计算结果作为参数传入，最后将其结果汇总为单个返回值。 -->
		<!-- 合并到单个数组  -->
		<script>
						var total = [1, 2, 3].reduce(function (sum, current) {
							return sum + current
						}, 0)
						console.log(total, 'total') //6

						var geyao = [
							{
								name: 'xiao hua',
								age: '18',
							},
							{
								name: 'xiao hong',
								age: '20',
							},
							{
								name: 'xiao gang',
								age: '16',
							},
							{
								name: 'xiao li',
								age: '15',
							},
						]

						var points = {
							"xiaohuan": 500,
							"xiaohong": '男',
						}
						var fangfang = geyao.reduce(function (arr, item) {
							// 移除名字中的空格，用来获取对应的 points
							var key = item.name.replace(' ', '')
							// 如果geyao有points，则加上它，否则设置为0
							if (points[key]) {
								item.points = points[key]
							} else {
								item.points = 0
							}
							// 把wizard对象加入到新数组里
							arr.push(item)

							// 返回这个数组
							return arr
						}, [])

			            console.log(fangfang,"fangfang") 
                        // [{	name: 'xiao hua',age: '18',points: 0},{age: "20",name: "xiao hong",points: "男"},
                        // {age: "16",name: "xiao gang",points: 0},{age: "15",,name: "xiao li",points: 0}]
		</script>
	</body>
</html>
